package com.fnzx.service.impl;

import com.aliyun.oss.internal.OSSUtils;
import com.fnzx.dao.ArticleDao;
import com.fnzx.entity.ArticleEntity;
import com.fnzx.service.ArticleService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import com.fnzx.util.oss.OssUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, ArticleEntity> implements ArticleService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                new QueryWrapper<ArticleEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils  queryPageByType(Map<String, Object> params) {
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<ArticleEntity>();
        if (params.get("type") != null && params.get("type") != "") {
            queryWrapper.eq("type",params.get("type"));
        }
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils  queryPageBy(Map<String, Object> params) {
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<ArticleEntity>();
        if (params.get("type") != null && params.get("type") != "") {
            queryWrapper.eq("type",params.get("type"));
        }
        if (params.get("search") != null && params.get("search") != "") {
            queryWrapper.and(wrapper ->wrapper.like("title", params.get("search")).or()
                    .like("author_name",params.get("search")).or()
                    .like("description",params.get("search")));
        }
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByTitle(Map<String, Object> params) {
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<ArticleEntity>();
        if (params.get("title") != null && params.get("title") != "") {
            queryWrapper.like("title",params.get("title"));
        }
        IPage<ArticleEntity> page = this.page(
                new Query<ArticleEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }


}